//
//  ViewController.m
//  TEST
//
//  Created by 李闯 on 15/12/2.
//  Copyright © 2015年 李闯. All rights reserved.
//

#import "ViewController.h"

#define kScreenWidth  [UIScreen mainScreen].bounds.size.width
@interface ViewController ()
{
    NSMutableArray *array;
    UIView *BGV;
    NSTimer *timer;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor yellowColor];
    
    [self addobjectforArray];
    BGV = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth / 2 - 40, 50,kScreenWidth / 2 + 40, 300)];
    [self.view addSubview:BGV];
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(autoMove) userInfo:nil repeats:YES];
    
}

- (void)autoMove
{
    static int i = 0;
    i++;
    if (i == array.count) {
        [timer  invalidate];
        return;
    }
    [self createLabelWithIndex:i];
    UILabel *label1 = [self.view viewWithTag:i + 100];
    UIImageView *userImage = [self.view viewWithTag:i + 10000];
    [UIView animateWithDuration:4 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        
        CGSize size = [array[i] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
        if (size.width > kScreenWidth / 2) {
            userImage.frame = CGRectMake(0, userImage.frame.origin.y - 300, 30, 30);
            label1.frame = CGRectMake(40, label1.frame.origin.y - 300, kScreenWidth / 2 - 10, 30);
            label1.numberOfLines = 0;
            [label1 sizeToFit];
        } else {
            userImage.frame = CGRectMake(kScreenWidth / 2 - size.width - 20,userImage.frame.origin.y - 300, 30, 30);
            label1.frame = CGRectMake(kScreenWidth / 2 - size.width - 20 + 40, label1.frame.origin.y - 300, size.width, 30);
            
        }
        
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
            [userImage removeFromSuperview];
            [label1 removeFromSuperview];
        } completion:^(BOOL finished) {
            
        }];
    }];
   
}

- (void)createLabelWithIndex:(int)num
{
    CGSize size = [array[num] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
    UILabel *label1 = [[UILabel alloc] init];
    UIImageView *userImage = [[UIImageView alloc] init];
    
    if (size.width > self.view.frame.size.width / 2) {
        
        label1.frame = CGRectMake(40, 300, kScreenWidth / 2 - 10, 30);
        userImage.frame = CGRectMake(0, 300, 30, 30);
    } else {
        label1.frame = CGRectMake(kScreenWidth / 2 - size.width - 20 + 40, 300, size.width, 30);
        userImage.frame = CGRectMake(kScreenWidth / 2 - size.width - 20, 300, 30, 30);
    }
    
    label1.text = array[num];
    label1.tag = num + 100;
    label1.numberOfLines = 0;
    [label1 sizeToFit];
    [BGV addSubview:label1];
    
    userImage.backgroundColor = [UIColor redColor];
//    userImage.image = [UIImage imageNamed:@"12"];
    userImage.tag = 10000 + num;
    userImage.layer.cornerRadius = 15;
    userImage.layer.masksToBounds = YES;
    [BGV addSubview:userImage];
    

}

- (void)addobjectforArray
{
    array = [NSMutableArray arrayWithObjects:@"阿娇北京哈表示将把精神病擦拭",@"看随随便便说你才是你",@"可很快就看空间",@"嫁鸡随鸡今生今世说嫁鸡随鸡时间就是今生今世就是结局",@"坎坎坷坷",@"谁是谁试试",@"啦是啦啦",@"哈哈萨达萨达哈",@"你你你你你你你你啊小拉开了大水坑里大哭",@"吃那难吃难吃难吃吃难吃难吃难吃难吃难吃",@"呵呵呵呵呵呵恍恍惚惚红红火火是双手合十", nil];
    //
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
